/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-07-07     17247       the first version
 */

/*
 * uart1_service.h - UART1 custom protocol for display and peripheral control
 *
 * This header defines the protocol frame, command codes, and API for sending data to a serial display or peripherals via UART1.
 *
 * Protocol Frame Structure:
 * [HEAD][LEN][CMD][DATA...][RESV][CHK][TAIL]
 *  1B   1B   1B   N      M     1B   1B
 *
 * HEAD: 0xA5 - Frame header
 * LEN:  Length of CMD+DATA+RESV (not including HEAD, LEN, CHK, TAIL)
 * CMD:  Command code (see below)
 * DATA: Payload, 1~N bytes, meaning depends on CMD
 * RESV: Reserved/extension bytes (optional, for future use)
 * CHK:  Checksum (sum of LEN, CMD, DATA, RESV, modulo 256)
 * TAIL: 0x5A - Frame tail
 *
 * Example: Send heart rate 80 (0x50):
 *   0xA5 0x02 0x01 0x50 0x53 0x5A
 *   (HEAD LEN CMD DATA CHK TAIL)
 *
 * All macros and functions are provided for easy expansion and maintenance.
 */

#ifndef APPLICATIONS_UART1_SERVICE_H_
#define APPLICATIONS_UART1_SERVICE_H_

#include <rtthread.h>
#include <rtdevice.h>
#include <stdint.h>

// Protocol constants
#define UART1_FRAME_HEAD   0xA5   // Frame header
#define UART1_FRAME_TAIL   0x5A   // Frame tail
#define UART1_FRAME_MAXLEN 16     // Max total frame length (adjust as needed)
#define UART1_RESV_LEN     0      // Reserved bytes (set >0 for future expansion)

// Command codes (CMD)
#define UART1_CMD_HEART_RATE   0x01 // Display heart rate, DATA: 1B (0~255)
#define UART1_CMD_BREATH_RATE  0x02 // Display breath rate, DATA: 1B (0~255)
#define UART1_CMD_STATUS       0x03 // Display status, DATA: 1B (0=normal, 1=alarm, ...)
#define UART1_CMD_BUZZER_CTRL  0x10 // Control buzzer, DATA: 1B (0=off, 1=on)
#define UART1_CMD_RELAY_CTRL   0x11 // Control relay, DATA: 1B (0=off, 1=on)
#define UART1_CMD_EXTEND       0x20 // Reserved for extension, DATA: N

/**
 * @brief Send a custom protocol frame via UART1.
 *
 * @param cmd      Command code (see macros above)
 * @param data     Pointer to data buffer (can be NULL if no data)
 * @param data_len Length of data buffer (0~13 recommended)
 *
 * Example:
 *   uint8_t hr = 80;
 *   uart1_send_frame(UART1_CMD_HEART_RATE, &hr, 1);
 */
void uart1_send_frame(uint8_t cmd, const uint8_t *data, uint8_t data_len);

#endif /* APPLICATIONS_UART1_SERVICE_H_ */
